package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {
    @Autowired
    private TravelCommentRepository travelCommentRepository;

    @Autowired
    private MongoTemplate mongoTemplate;//可以操作数据库

    @Autowired
    private IDestinationService destinationService;//目的地服务类

    @Autowired
    private IUserInfoService userInfoService;



    @Override
    public TravelComment get(String id) {
        Optional byId = travelCommentRepository.findById(id);
        return (TravelComment) byId.orElse(null);//如果是空是就返回空值,否则返回对象
    }

    @Override
    public void save(TravelComment travelComment ) {
        travelComment.setId(null);
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void commentAdd(TravelComment comment) {

    }

    @Override
    public void addComment(TravelComment comment, UserInfo userInfo) {
        //评论发表时间是提交的那一刻时间,不用用户填写的
        comment.setCreateTime(new Date());
        //表示把(源对象userInfo的属性值设置给目标对象travelComment的同名属性变量)
        //目的是当前登录用户添加的评论.所以评论对象的用户名称和用户等级等等和当前登录对象的是一样的()
        BeanUtils.copyProperties(userInfo,comment);
        //因为用户对象id和评论对象的uerId不一样,要单独设置
        //是用户对象评论的,所以评论对象的userId和用户对象的id是一样的

        // 使用了  BeanUtils.copyProperties必须放在设置id为null之前
        // 新增方法必须设置id为空后,不设置id有值,才能执行新增操作,不然id有值了就执行更新操作
        comment.setId(null);
        comment .setUserId(userInfo.getId());
        //添加到数据库
        travelCommentRepository.save(comment);
    }


    // 添加游记评论操作




    @Override
    public List<TravelComment> listAll() {
        List<TravelComment> travelComments = travelCommentRepository.findAll();
        return travelComments;
    }




    @Override
    public Page<TravelComment> query(TravelCommentQuery qo) {
        //查询对象
        Query query = new Query();
        //根据id过滤出这个攻略下的所有的评论
        if(StringUtils.hasLength(qo.getTravelId())) {
            Criteria criteria = Criteria.where("travelId").is(qo.getTravelId());
            query.addCriteria(criteria);
        }
        //查询出总数条数
        long rows = mongoTemplate.count(query, TravelComment.class);
        if (rows == 0) {
            return  Page.empty();//如果总数条数是0,就返回空集合
        }
        //分页对象(设置当前页从0开始算,还有页面显示条数,按sequence这个列正序排列 )  
        //  Sort.Direction.ASC,表示按正序排列表格
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.DESC, "createTime");
        //给查询对象
        query.with(pageable);


        List<TravelComment> list = mongoTemplate.find(query, TravelComment.class);
        PageImpl<TravelComment> page = new PageImpl<>(list, pageable, rows);


        return page ;//相当于pageResult对象

    }



}
